package com.mrd.gtimp.v2.datalock;

import com.alibaba.fastjson.JSONObject;
import com.mrd.util.LoginUtil;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/datalock")
public class DataLockAction {
    @Resource
    private DataLockUtil dataLockUtil;

    @RequestMapping("/lock")
    @ResponseBody
    public JsonResult lock(String opaId, String bidId) {
        if ("CL".equals(LoginUtil.getLoginUser().getCompany().getType())) {
            return new JsonResult("200", "CL。");
        }
        if (dataLockUtil.lock(opaId, bidId, LoginUtil.getLoginUser().getAccount() + LoginUtil.getLoginUser().getName())) {
            return new JsonResult("200", "业务锁定成功。");
        } else {
            return new JsonResult("201", "当前业务" + dataLockUtil.getLockInfo(opaId, bidId).getRegUser() + "正在操作中。");
        }
    }

    @RequestMapping("/tryLock")
    @ResponseBody
    public JsonResult tryLock(String opaId, String bidId) {
        String regUser = LoginUtil.getLoginUser().getAccount() + LoginUtil.getLoginUser().getName();
        if (dataLockUtil.tryLock(opaId, bidId, regUser)) {
            return new JsonResult("200", "业务可以锁定。");
        } else {
            return new JsonResult("201", "当前业务" + dataLockUtil.getLockInfo(opaId, bidId).getRegUser() + "正在操作中。");
        }
    }

    @RequestMapping("/onUnlock")
    @ResponseBody
    public JsonResult onUnlock(String opaId, String bidId) {
        /*if ("CL".equals(LoginUtil.getLoginUser().getCompany().getType())) {
            return new JsonResult("200", "CL。");
        }*/
        dataLockUtil.releaseLock(opaId, bidId);
        return new JsonResult("200", "OK");
    }

    @RequestMapping("/unlockDV")
    @ResponseBody
    public JsonResult unlockDV(String opaId, String bidId) {
        /*if ("CL".equals(LoginUtil.getLoginUser().getCompany().getType())) {
            return new JsonResult("200", "CL。");
        }*/
        dataLockUtil.releaseLock(dataLockUtil.getOpaIDByKey("opa.view_declaration_by_sup"), bidId);
        return new JsonResult("200", "OK");
    }

    @RequestMapping("/list")
    @ResponseBody
    public JsonResult list(String opaId, String bidId) {
        return new JsonResult("200", JSONObject.toJSONString(dataLockUtil.list()));
    }

    @RequestMapping("/datalockM")
    public String datalockM() {
        return "/datalockM";
    }
}
